<script>
	import {
		profile,
		register,
		setting
	} from "@/common/api.js"
	export default {
		onLaunch: function() {

			// #ifdef APP-PLUS
			plus.screen.lockOrientation("portrait-primary")
			// #endif

			uni.hideTabBar();
			// #ifdef MP-WEIXIN
			this.getOpenId("");
			//   #endif
			this.getAiList();
			console.log('App Launch')
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {

			// 获取配置列表
			async getAiList() {
				const res = await setting();
				if (res.code == 1) {
					uni.removeStorageSync('setting');
					uni.setStorageSync('setting', JSON.stringify(res.data));
				}
			},
			// 是否完善信息
			isCompleteInfo() {
				const userinfo = uni.getStorageSync('userinfo');
				if (userinfo) {
					let {
						telephone,
						birthday,
						gender
					} = JSON.parse(userinfo);
					console.log(telephone, birthday, gender)
					if (!telephone || !birthday || !gender) {
						uni.showModal({
							title: "提示",
							content: "请先完善个人信息,是否前往?",
							success: (res) => {
								if (res.confirm) {
									uni.navigateTo({
										url: "/pages/mine/mineInfo/mineInfo"
									})

								} else if (res.cancel) {

								}
							}
						});
						return false;
					}
				}
				return true;
			},
			getOpenId(toast = '登录成功') {
				uni.login({
					provider: 'weixin'
				}).then(async res => {
					console.log("查看code", res)
					const result = await register({
						code: res.code
					});
					if (result.code === 1) {
						console.log("注册成功", toast)
						uni.setStorageSync("token", result.token);
						this.getMineInfo();
						this.getAiList();
						if (toast) {
							uni.showToast({
								title: toast,
								icon: 'success',
								duration: 1500,
								success: () => {
									uni.switchTab({
										url: "/pages/index/index"
									})
								}
							})
						}
					} else {
						console.log("注册失败", result)
					}
				}).catch(err => {
					console.log(err)
				})
			},
			// 获取用户信息
			async getMineInfo() {
				let token = uni.getStorageSync('token');
				if (token) {
					const res = await profile();
					if (res.code == 1) {
						uni.setStorageSync('userinfo', JSON.stringify(res.data));
						return res.data;
					}
				}
				uni.showModal({
					title: "提示",
					content: "请先登录",
					success: (res) => {
						if (res.confirm) {
              // #ifdef MP-WEIXIN
							  this.getOpenId();
              // #endif
              // #ifdef APP-PLUS || H5
                uni.navigateTo({
                  url: "/pages/login/login"
                });
              // #endif
						} else if (res.cancel) {
              uni.switchTab({
                url: "/pages/index/index"
              });
						}
					}
				});
				return false;
			},
			isLogin(callback) {
				let token = uni.getStorageSync('token') || "";
				if (token) {
					callback && callback();
				} else {
					uni.showModal({
						title: "提示",
						content: "请先登录",
						success: (res) => {
							if (res.confirm) {
                // #ifdef MP-WEIXIN
                this.getOpenId();
                // #endif
                // #ifdef APP-PLUS || H5
                uni.navigateTo({
                  url: "/pages/login/login"
                });
                // #endif
							} else if (res.cancel) {
                uni.switchTab({
                  url: "/pages/index/index"
                });
							}
						}
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "uview-ui/index.scss";
	@import 'colorui/animation.css';
	@import "colorui/main.scss";
	@import "colorui/common.scss";

	page {
		background: #F5F6FA;
	}

	.main-card {
		background: #fff;
		margin: 0 30rpx;
		border-radius: 20rpx;
	}

	.main-btn {
		/deep/ .u-button {
			border-radius: 20rpx !important;
		}
	}

	.popup-cell {
		.cell-item {
			background: #F2F2F2;
			padding: 22rpx 30rpx;
			border-radius: 20rpx;
			font-size: 26rpx;

			input {
				color: #333;
			}
		}

		/deep/ .placeholder-class {
			font-size: 26rpx;
			color: #999;
		}
	}

	.black-color {
		color: #000;
	}

	.blue-color {
		color: #5468EA;
	}

	.green-color {
		color: #399567;
	}

	.yellow-color {
		color: #E48914;
	}

	.brown-color {
		color: #B7550D;
	}

	.red-color {
		color: #EE2824;
	}
</style>